package com.lzz.bottomencapsulation.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by apple on 2017/12/21.
 */

public class TimeUtil {

    public static String HH = "HH";
    public static String mm = "mm";
    public static String Hms = "HH:mm:ss";
    public static String Hm = "HH:mm";
    public static String ms = "mm:ss";
    public static String MdHm = "MM-dd HH:mm";
    public static String yMdHms = "yyyy-MM-dd HH:mm:ss";
    public static String yMdHms2 = "yyyy/MM/dd HH:mm:ss";
    public static String yMdHm = "yyyy-MM-dd HH:mm";
    public static String yMdHm2 = "yyyy/MM/dd HH:mm";
    public static String yMd = "yyyy-MM-dd";
    public static String yMdStr = "yyyy年MM月dd日";
    public final static String PATTERN_Md = "MM.dd";
    public final static String PATTERN_d = "dd";
    public final static String HMStr = "HH 小时 mm 分钟";

    /**
     * 获取时间戳  根据时间情况 时分秒
     *
     * @param time
     * @return
     */
    public static String getTextHms(long time) {
        time = time - TimeZone.getDefault().getRawOffset();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        SimpleDateFormat h = new SimpleDateFormat(HH);
        if (h.format(calendar.getTime()).equals("00")) {
            return getTextms(time);
        }
        SimpleDateFormat fmat = new SimpleDateFormat(Hms);
        return fmat.format(calendar.getTime());
    }

    /**
     * 获取时间戳   分秒
     *
     * @param time
     * @return
     */
    public static String getTextms(long time) {
        time = time - TimeZone.getDefault().getRawOffset();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        SimpleDateFormat fmat = new SimpleDateFormat(ms);
        return fmat.format(calendar.getTime());
    }

    /**
     * 获取时间戳
     */
    public static long getTimeMliiis() {
        return System.currentTimeMillis();
    }

    public static long getTimeMliiisYMD() {
        long time = System.currentTimeMillis();
        return getTimeMliiis(yMdHms, getText(yMd, time));
    }

    public static long getTimeMliiis(String type, String time) {
        long t = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        Date d;
        try {
            d = sdf.parse(time);
            t = d.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block e.printStackTrace();
        }
        return t;
    }


    /**
     * 转成指定格式
     *
     * @param type
     * @param time
     * @return
     */
    public static String getText(String type, long time) {
        SimpleDateFormat fmat = new SimpleDateFormat(type);
        return fmat.format(time);
    }

    public static String getMonthDayWeek(long time) {

        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(time));

        int year = cd.get(Calendar.YEAR); //获取年份
        int month = cd.get(Calendar.MONTH) + 1; //获取月份
        int day = cd.get(Calendar.DAY_OF_MONTH); //获取日期
        int week = cd.get(Calendar.DAY_OF_WEEK); //获取星期

        String weekString;
        switch (week) {
            case Calendar.SUNDAY:
                weekString = "星期日";
                break;
            case Calendar.MONDAY:
                weekString = "星期一";
                break;
            case Calendar.TUESDAY:
                weekString = "星期二";
                break;
            case Calendar.WEDNESDAY:
                weekString = "星期三";
                break;
            case Calendar.THURSDAY:
                weekString = "星期四";
                break;
            case Calendar.FRIDAY:
                weekString = "星期五";
                break;
            default:
                weekString = "星期六";
                break;

        }

        weekString = month + "月" + day + "日" + weekString;

        return weekString;
    }

    public static String getWeek(long time) {

        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(time));

        int year = cd.get(Calendar.YEAR); //获取年份
        int month = cd.get(Calendar.MONTH); //获取月份
        int day = cd.get(Calendar.DAY_OF_MONTH); //获取日期
        int week = cd.get(Calendar.DAY_OF_WEEK); //获取星期

        String weekString;
        switch (week) {
            case Calendar.SUNDAY:
                weekString = "日";
                break;
            case Calendar.MONDAY:
                weekString = "一";
                break;
            case Calendar.TUESDAY:
                weekString = "二";
                break;
            case Calendar.WEDNESDAY:
                weekString = "三";
                break;
            case Calendar.THURSDAY:
                weekString = "四";
                break;
            case Calendar.FRIDAY:
                weekString = "五";
                break;
            default:
                weekString = "六";
                break;

        }

        return weekString;
    }


    public static long getNextDay(String time, int nextday) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + nextday);
        LUtils.e(date + "=============================" + c.getTime() + "+++++++++++++++++++++");
        LUtils.e("StartDay", date);
        LUtils.e("LastDay", c.getTime());
        return c.getTime().getTime();
    }

    /**
     * 根据时间戳获取应该显示的时间
     *
     * @param time
     * @return
     */
    public static String getTextTime(long time) {
//        time = time - TimeZone.getDefault().getRawOffset();
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeInMillis(time);
//        SimpleDateFormat h = new SimpleDateFormat(HH);
//        if (h.format(calendar.getTime()).equals("00")) {
//            return getTextms(time);
//        }
//        SimpleDateFormat fmat = new SimpleDateFormat(Hms);
//        return fmat.format(calendar.getTime());

        if (time >= 3600 * 1000) {
//            大于等于1小时
            time = time - TimeZone.getDefault().getRawOffset();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(time);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(HH);
            int h = Integer.parseInt(simpleDateFormat.format(calendar.getTime()));
            return h + "小时";
        } else if (time <= 60 * 1000) {
//            小于等于1分钟
            return "一分钟";
        } else {
            time = time - TimeZone.getDefault().getRawOffset();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(time);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(mm);
            int h = Integer.parseInt(simpleDateFormat.format(calendar.getTime()));
            return h + "分钟";
        }
    }

    public static String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat(Hm);
        return format.format(date);
    }

    public static Date getDate(String time) {//可根据需要自行截取数据显示

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(yMd);
        String dateNowStr = sdf.format(d);
        SimpleDateFormat format = new SimpleDateFormat(yMdHm);
        Date date = null;
        try {
            date = format.parse(dateNowStr+" "+time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取精确到秒的时间戳
     * @return
     */
    public static int getSecondTimestamp(Date date){
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return Integer.valueOf(timestamp.substring(0,length-3));
        } else {
            return 0;
        }
    }


    /**
     * 通知--实时风险--时间显示
     */

    public static String getTimeSTR(long timeInterval){
        String str="";
        if (timeInterval<60) {
            str="刚刚";
        }else if ((timeInterval/60)<60){
            str=timeInterval/60+"分钟前";
        }else if ((timeInterval/(60*60))<24){
            str=timeInterval/(60*60)+"小时前";
        }else if((timeInterval/(24*60*60))<7){
            str=timeInterval/(24*60*60)+"天前";
        }else{
            str= getText(yMdHms,timeInterval*1000);
        }
        return str;
    }

}
